{% extends "base.html" %}
{% load static %}
{% load widget_tweaks %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>数据订阅列表</h2>
        <a href="{% url 'subscription_create' %}" class="btn btn-primary">
            <i class="bi bi-plus"></i> 添加订阅
        </a>
    </div>

    {# 过滤器 #}
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0">筛选条件</h5>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <label for="{{ filter.form.data_source.id_for_label }}" class="form-label">数据源</label>
                    {{ filter.form.data_source|add_class:"form-control" }}
                </div>
                <div class="col-md-4">
                    <label for="{{ filter.form.node_id.id_for_label }}" class="form-label">节点ID/主题</label>
                    {{ filter.form.node_id|add_class:"form-control" }}
                </div>
                <div class="col-md-2">
                    <label for="{{ filter.form.is_active.id_for_label }}" class="form-label">状态</label>
                    {{ filter.form.is_active|add_class:"form-control" }}
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100">筛选</button>
                </div>
            </form>
        </div>
    </div>

    {# 订阅列表 #}
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>数据源</th>
                    <th>节点ID/主题</th>
                    <th>报警条件</th>
                    <th>阈值</th>
                    <th>单位</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for subscription in subscriptions %}
                <tr>
                    <td>{{ subscription.data_source.name }}</td>
                    <td>{{ subscription.node_id }}</td>
                    <td>{{ subscription.get_condition_display }}</td>
                    <td>
                        {% if subscription.condition == 'range' %}
                            {{ subscription.threshold_value }} - {{ subscription.threshold_max }}
                        {% else %}
                            {{ subscription.threshold_value }}
                        {% endif %}
                    </td>
                    <td>{{ subscription.unit|default:"-" }}</td>
                    <td>
                        {% if subscription.is_active %}
                            <span class="badge bg-success">启用</span>
                        {% else %}
                            <span class="badge bg-secondary">禁用</span>
                        {% endif %}
                    </td>
                    <td>
                        <div class="btn-group" role="group">
                            <a href="{% url 'subscription_update' subscription.id %}" 
                               class="btn btn-sm btn-outline-primary">
                                编辑
                            </a>
                            <a href="{% url 'subscription_delete' subscription.id %}" 
                               class="btn btn-sm btn-outline-danger"
                               onclick="return confirm('确定要删除这个订阅吗？')">
                                删除
                            </a>
                        </div>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="7" class="text-center text-muted">暂无订阅数据</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if is_paginated %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page=1">&laquo; 首页</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}">上一页</a>
            </li>
            {% endif %}

            <li class="page-item active">
                <span class="page-link">
                    第 {{ page_obj.number }} 页 / 共 {{ page_obj.paginator.num_pages }} 页
                </span>
            </li>

            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}">下一页</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">末页 &raquo;</a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %} 